A brief introduction to Jupyter Notebooks for Python.

This is a Jupyter notebook. Python is a programming language, and a Jupyter notebook is an interactive document which contains text (like what you're reading now) and Python code. Jupyter notebooks will be used throughout the tutorial, and this is a brief introduction to the interface.

Table of Contents

  1. Notebook cells
  2. Markdown syntax
  3. Saving, loading, printing

Notebook cells

Click inside the cell below, in the box to the right of the In []: prompt. A border should appear around the cell, with a green bar at the left, indicating that it's active. Then press shift-Enter (hold down the shift key while pressing the Enter key) on the keyboard to evaluate the cell.


In [ ]:
2+2

If an output message like Out[1]: 4 appeared, then everything is working correctly!

Jupyter notebooks are built out of cells. There are two primary types of cells: Markdown cells and Code cells. Textual content will be contained in Markdown cells like this one. Programming content will be contained in Code cells, like the 2+2 cell above.

If a cell has a green border, that means that you're currently editing it. If it has a blue border, that means that the cell is selected (and can be cut and pasted, etc.) but not in edit-mode. Otherwise it is not active.

To edit a Code cell, click anywhere within its evaluation box. For example, you can click inside the 2+2 cell, change the numbers to 2+3 and re-evaluate with shift-Enter.

To edit a Markdown cell like this one, double-click anywhere within its body. You can write Markdown just like any text document, for the most part. When you're done editing a Markdown cell, press shift-Enter.

To change a selected cell from Code to Markdown or back again, you should see a dropdown menu with "Markdown" or "Code" visible, just above your notebook interface. Just drop down to select the type of the cell. For practice, select the cell below to a Markdown cell, and write a sentence or two. Complete it with a shift-Enter.


In [ ]:
#  Change this cell to a Markdown cell, and write something.

Now, change the cell below to a Code cell, and use it to compute 999 * 999.

(I'm a markdown cell. Change me to a Code cell and compute 999*999)

Often you'll want to insert a new cell above or below a given cell. To do this, you need to select the cell in one of the following ways. Select a Code cell with a single click outside of its evaluation box, e.g., click where it says In [ ]:. Select a Markdown cell with a single-click anywhere in its body.

If you have selected the cell correctly, a blue border should be visible around it. Once the blue border is visible, you can add a new cell above it by pressing the a key on the keyboard. You can add a new cell below it by pressing the b key on the keyboard. Try to add a new cell above and below the Markdown cell below. The newly created cells are probably Code cells by default.

Instead of a or b, you can also press x. That will delete the cell, so be careful and save often.

(I'm a markdown cell. Add a new cell above and below me.)

A quick note: there are a few invisible cells in this notebook, and in other notebooks in the tutorial. In fact, one of these sneaky cells is just below this one! It's a Markdown cell, as you will see if you double-click it. Its content is <a id='markdown'></a>, which is a snippet of HTML code. This is a little placeholder, which enables the links in the table of contents. Don't change those Markdown cells, unless you want to break the links in the table of contents.

Basic Markdown for Jupyter notebooks

You will learn more about Python code in the tutorial. As you are learning, you should experiment in code cells, and write answers to questions and other notes in markdown cells. Markdown is a neat text-editing language, a sort of shorthand that extends HTML. Here is a brief guide to typing in Markdown.

Most of the time, you can type Markdown just like any old text document. One little difference is that to start a new line, you have to insert an extra empty line in the Markdown. Double-click this cell to see how the new paragraph was started below.

Markdown has many more features that you'll notice in this document: there are different typefaces, lists, headings, links, and more.

Typefaces

To change from plain text to italics or to boldface, you surround the text by asterisks or double-asterisks. Double-click inside this Markdown cell to see how it was done! Then insert a new Markdown cell below this one to experiment. Strictly speaking, the single-asterisks are used to convert to the HTML <em> tag, which places emphasis on text. The double-asterisks convert to the HTML <strong> tag, which is often boldfaced. But different web-browsers behave differently.

Lists

Often it is useful to make lists. The two kinds of lists we'll use are numbered lists and unnumbered lists. These are easy in Markdown. For a numbered list, just start each new line with a number. Double-click this cell to see how it was done.

  1. This is the first line.
  2. This is the second line.
  3. This is the third line.

For an unnumbered (bulleted) list, start each new line with a dash - (a few other symbols work too). Double-click this cell to see how it was done.

  • This is the first line.
  • This is the second line.
  • This is the third line.

Now I'm back to ordinary text again.

You can nest your lists. But people get too attached to nested hierarchies in their writing and presentations. Lists have their place, but paragraphs should be more common. If you must nest a list, double-click this cell to see how it was done, with a bit of indentation.

  • This is the first line.
    • This is the first subline within the first line.
    • This is the second subline within the first line.
  • This is the second line.
  • This is the third line.

Now I'm back to ordinary text again.

Headings

There are different levels of headings, i.e., headings, subheadings, etc., which typically render as boldface text of different sizes. To make a top-level heading, a single hashtag # is used. The next level heading is created with a double hashtag ##, and so on. Double-click this cell to see how it was done.

A top-level heading.

The next-level heading.

A third-level heading.

A fourth-level heading.

Finally, we discuss how to insert web-links in your Markdown code. For example, here is a link to the webpage to buy An Illustrated Theory of Numbers. The general syntax is that the displayed text goes in brackets followed by the URL (web address) in parentheses. Double-click this cell to see how it was done.

Conclusion

There is much much more you can do with Markdown. In fact, Markdown is a superset of HTML, so you can put just about any basic web content in a Markdown cell. A quick google search will turn up lots of Markdown guides, and it's best to search for Markdown and Jupyter to find a guide for Jupyter's particular flavor of Markdown (which differs a bit from WordPress markdown, for example).

Saving, loading, and printing notebooks

You can save, load, and print Jupyter notebooks. In fact, it's probably a good time to save this notebook since you've edited the markdown so far. Go to "File...Save and Checkpoint" (the File menu just under "Jupyter") to save your notebook so far.

The standard notebook file format is .ipynb which stands for "iPython Notebook", since Jupyter used to be called iPython. If you go to "File...Download as...", then "IPython Notebook" will be the top choice. You will always be able to reload your notebook and edit it, if you save it as an iPython Notebook.

You may also want to save a copy as an .html file. HTML is the code used to render webpages, and so it's good for sharing with anyone with a web browser. Also, your web browser can probably print the HTML file nicely, so it's a good format for printing. You can go directly to "File...Print preview" to see what the notebook will look like when printed.

To open a Python notebook, you can go to "File...Open" or you can return to your original Jupyter tab (if it's still there) and browse directories to find notebook files. But Jupyter must be started to load python notebooks on your computer.

One more tool to learn about is GitHub. GitHub is a website which allows users to share and develop software of all sorts. It's used for collaborative software development, and has many desirable features like "versioning" (keeping track of versions along the way as various parties make edits). You can freely use GitHub to store your Python notebooks. GitHub takes a bit of practice, but it's worth the effort in the long run.

The tutorial notebooks are stored on GitHub for a few reasons. First, they can be easily edited and improved. Second, they become freely and publicly available. Go to my GitHub page to see them! Third, even though they are stored in .ipynb format, anyone can view Python notebooks on the web using the Jupyter Notebook Viewer. For example, you can use the Jupyter Notebook Viewer to see this notebook online.

Now you know more than enough to begin the Python tutorials. Download the notebooks and happy programming!